package com.base.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.system.domain.bo.TSysRoleMenuBo;
import com.base.system.domain.vo.TSysRoleMenuVo;
import com.base.system.mapper.TSysRoleMenuMapper;
import com.base.system.service.ITSysRoleMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.system.domain.TSysRoleMenu;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 角色菜单权限Service业务层处理
 *
 * @author base
 * @date 2023-10-18
 */
@RequiredArgsConstructor
@Service
public class TSysRoleMenuServiceImpl implements ITSysRoleMenuService {

    private final TSysRoleMenuMapper baseMapper;

    /**
     * 查询角色菜单权限
     */
    @Override
    public TSysRoleMenuVo queryById(String autoId){
        return baseMapper.selectVoById(autoId);
    }

    /**
     * 查询角色菜单权限列表
     */
    @Override
    public TableDataInfo<TSysRoleMenuVo> queryPageList(TSysRoleMenuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSysRoleMenu> lqw = buildQueryWrapper(bo);
        Page<TSysRoleMenuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询角色菜单权限列表
     */
    @Override
    public List<TSysRoleMenuVo> queryList(TSysRoleMenuBo bo) {
        LambdaQueryWrapper<TSysRoleMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSysRoleMenu> buildQueryWrapper(TSysRoleMenuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSysRoleMenu> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getRoleId()), TSysRoleMenu::getRoleId, bo.getRoleId());
        return lqw;
    }

    /**
     * 新增角色菜单权限
     */
    @Override
    public Boolean insertByBo(TSysRoleMenuBo bo) {
        TSysRoleMenu add = BeanUtil.toBean(bo, TSysRoleMenu.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setAutoId(add.getAutoId());
        }
        return flag;
    }

    /**
     * 修改角色菜单权限
     */
    @Override
    public Boolean updateByBo(TSysRoleMenuBo bo) {
        TSysRoleMenu update = BeanUtil.toBean(bo, TSysRoleMenu.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSysRoleMenu entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除角色菜单权限
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
